/**
 * @(#)TimeModelParentTempleteService.java 2017年2月9日
 * Copyright(C) 2017 ZHEXIN IT CO.,LTD. All rights reserved.
 */
package org.jsbd.boss.service.impl;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.jsbd.boss.common.redis.RedisClient;
import org.jsbd.boss.dao.ITimeModelTempleteDAO;
import org.jsbd.boss.domian.channel.TimeBaseModel;
import org.jsbd.boss.domian.channel.TimeModelTemplete;
import org.jsbd.boss.service.ITimeModelTempleteService;
import org.jsbd.boss.util.CacheKey;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

/**
 * TODO 请加入本类的说明
 * 
 * @author <a href="mailto:xupengcheng@zhexinit.com" >徐鹏程</a>
 * @version 1.0.0
 */
@Service
public class TimeModelTempleteService implements ITimeModelTempleteService {
	public static final Logger LOGGER = LoggerFactory.getLogger(TimeModelTempleteService.class);

	@Resource
	private ITimeModelTempleteDAO timeModelTempleteDAO;

	@Resource
	private RedisClient<String, List<TimeBaseModel>> redisClient;

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.jsbd.boss.service.ITimeModelParentTempleteService#insertTemplete(
	 * org.jsbd.boss.domian.channel.TimeModelParentTemplete)
	 */
	@Override
	public Integer insertTemplete(TimeModelTemplete templete) {
		return timeModelTempleteDAO.insertTemplete(templete);
	}

	@Override
	public void insertTempleteList(List<TimeModelTemplete> templeteList) {
		for (TimeModelTemplete templete : templeteList) {
			timeModelTempleteDAO.insertTemplete(templete);
		}
	}

	@Override
	public List<TimeModelTemplete> selectTemplete(TimeModelTemplete form) {
		if (form.getParentId() == null) {
			return new ArrayList<>();
		}
		List<TimeModelTemplete> list = timeModelTempleteDAO.selectTemplete(form);
		if (list == null || list.size() == 0) {
			List<TimeBaseModel> baseList = selectAllBaseModel();
			if (baseList != null && baseList.size() > 0) {
				list = new ArrayList<TimeModelTemplete>();
				for (TimeBaseModel baseModel : baseList) {
					TimeModelTemplete modelTemplete = new TimeModelTemplete();
					modelTemplete.setParentId(form.getParentId());
					modelTemplete.setCurrRate(baseModel.getCurrRate());
					modelTemplete.setTotalRate(baseModel.getTotalRate());
					modelTemplete.setRateTime(baseModel.getRateTime());
					list.add(modelTemplete);
				}
			}
		}
		return list;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.jsbd.boss.service.ITimeModelParentTempleteService#updateTemplete(
	 * org.jsbd.boss.domian.channel.TimeModelParentTemplete)
	 */
	@Override
	public Integer updateTemplete(TimeModelTemplete templete) {
		return timeModelTempleteDAO.updateTemplete(templete);
	}

	@Override
	public List<TimeBaseModel> selectAllBaseModel() {
		Object baseModel = redisClient.get(CacheKey.B_MODEL);
		if (baseModel != null) {
			return (List<TimeBaseModel>) baseModel;
		} else {
			List<TimeBaseModel> list = timeModelTempleteDAO.selectAllBaseModel();
			if (list != null) {
				redisClient.put(CacheKey.B_MODEL, list);
			}
			return list;
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.jsbd.boss.service.ITimeModelTempleteService#saveTimeModelList(java
	 * .util.List)
	 */
	@Override
	public void saveTimeModelList(List<TimeModelTemplete> list) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String now = sdf.format(new Date());
		for (TimeModelTemplete templete : list) {
			String createTime = templete.getCreateTime();
			String modifyTime = templete.getModifyTime();
			if (StringUtils.isBlank(createTime)) {
				templete.setCreateTime(now);
			}
			if (StringUtils.isBlank(modifyTime)) {
				templete.setModifyTime(now);
			}
			timeModelTempleteDAO.insertTemplete(templete);
		}
	}
}
